import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message } from 'element-ui'

Vue.use(VueRouter)

const routes = [
	{
		path: '/',
		redirect: '/home',
	},
	{
		path: '/home',
		name: 'home',
		component: () => import('views/home/home.vue'),
	},
	{ path: '/login', component: () => import('views/login/login.vue') },
	{
		path: '/register',
		component: () => import('views/register/register.vue'),
	},
	{
		path: '/order/',
		component: () => import('views/order/order.vue'),
		meta: { requiresAuth: true },
	},

	{
		path: '/logistic',
		component: () => import('views/logistic/logistic.vue'),
		meta: { requiresAuth: true },
	},
	{ path: '/shop', component: () => import('views/shop/shop.vue') },
	{
		path: '/personInfo',
		component: () => import('views/personInfo/personInfo.vue'),
		meta: { requiresAuth: true },
	},
	{
		path: '/shoppingCart',
		component: () => import('views/shoppingCart/shoppingCart.vue'),
		meta: { requiresAuth: true },
	},
	{
		path: '/buy',
		component: () => import('views/buy/buy.vue'),
	},
	{
		path: '/seller',
		component: () => import('views/seller/home.vue'),
	},
]

const router = new VueRouter({
	mode: 'history',
	routes,
})

// 路由导航守卫
const pathToken = new Set([
	'/personInfo',
	'/shoppingCart',
	'/logistic',
	'/order',
])

router.beforeEach((to, from, next) => {
	if (to.meta.requiresAuth) {
		const userId = sessionStorage.getItem('userId')
		if (!userId) {
			Message({
				message: '请先登录',
				type: 'warning',
			})
			return next('/login')
		}
	}
	next()
})

export default router
